package com.sprint.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sprint.entities.CommonResult;
import com.sprint.entities.Payment;
import com.sprint.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "global_errorFunc")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentinfo_ok(@PathVariable("id") Integer id)
    {
        return paymentHystrixService.paymentinfo_ok(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod="paymentInfo_TimeOutHandler" ,commandProperties={
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value ="3000")
    })
    //单一方法降级处理
    */@HystrixCommand
    public String paymentinfo_timeout(@PathVariable("id") Integer id)
    {
        return paymentHystrixService.paymentinfo_timeout(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout2/{id}")
    public String paymentinfo_timeout2(@PathVariable("id") Integer id)
    {
        return paymentHystrixService.paymentinfo_timeout(id);
    }



    //兜底方法
    public String paymentInfo_TimeOutHandler(@PathVariable("id")  Integer id){
        return "线程"+Thread.currentThread().getName() + " 系统异常，请稍后再试试，单一 fallback处理方法";

    }


    public String global_errorFunc(){
        return "线程"+Thread.currentThread().getName() + " 系统异常，请稍后再试试， 全局 global_errorFunc fallback处理方法";

    }


}
